//
//  UserViewController.m
//  LiWuShuo
//
//  Created by 袁小航 on 14/11/12.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "UserViewController.h"
#import "User.h"
#import "Define.h"
#import "SetViewController.h"
#import "AppDelegate.h"
#import "Collect.h"
#import "CollectCell.h"
#import <UIImageView+AFNetworking.h>
#import "ProDetailViewController.h"

@interface UserViewController ()<UITableViewDataSource, UITableViewDelegate>

//创建视图
@property(nonatomic, retain) User *userView;

//定义一个数组 用来存储保存的礼物数据
@property(nonatomic, retain) NSMutableArray *userCollects;

//定义一个NSManagedObjectContext 类型的对象
@property(nonatomic, strong) NSManagedObjectContext *managedObjectContext;


@end

@implementation UserViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = NO;
//    使用Coredata
    AppDelegate *delegate = [UIApplication sharedApplication].delegate;
    self.managedObjectContext = delegate.managedObjectContext;
    
//    获取coredata中的数据
    [self getDataFromCoreData];

}

- (void)viewDidLoad {
    [super viewDidLoad];
//     Do any additional setup after loading the view.
    
//    设置navigation为透明
    self.navigationController.navigationBarHidden = YES;
    
//    创建userTableVIewView
    [self createUserView];
    
}

#pragma mark - 获取coredata中的数据
- (void)getDataFromCoreData
{
    NSFetchRequest *request = [[NSFetchRequest alloc]initWithEntityName:@"Collect"];
    self.userCollects = [NSMutableArray arrayWithArray:[self.managedObjectContext executeFetchRequest:request error:nil]];
}

#pragma mark - 创建userTableVIewView
- (void)createUserView
{
    self.userView = [[User alloc]initWithFrame:CGRectMake(0, 0, MAIN_SCREEN_WIDTH, MAIN_SCREEN_HEIGHT - 49)];
    _userView.userTableView.backgroundColor = [UIColor clearColor];
    [_userView.setButton addTarget:self action:@selector(setButtonAction) forControlEvents:UIControlEventTouchUpInside];
    
    _userView.userTableView.delegate = self;
    _userView.userTableView.dataSource = self;
    
    [self.view addSubview:_userView];
}
#pragma mark - tableView 的delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_userCollects.count == 0) {
        return 1;
    }
    return _userCollects.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_userCollects.count == 0) {
        static NSString *identifier = @"cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        
        cell.textLabel.textColor = [UIColor lightGrayColor];
        cell.textLabel.textAlignment = YES;
        cell.textLabel.text = @"在这里现实您收藏的礼物";
        
        return cell;
    } else {
        static NSString *collIdentifer = @"collCell";
        CollectCell *collCell = [tableView dequeueReusableCellWithIdentifier:collIdentifer];
        if (!collCell) {
            collCell = [[CollectCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:collIdentifer];
        }
        
        collCell.collInfoV.text = [_userCollects[indexPath.row] info];
        [collCell.collImageV setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@", [_userCollects[indexPath.row] image]]] placeholderImage:[UIImage imageNamed:@"placehold.png"]];
        collCell.collImageV.contentMode = UIViewContentModeScaleAspectFill;
        
        return collCell;
    }
    return nil;
}

#pragma mark - row的高度 
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_userCollects.count == 0) {
        return MAIN_SCREEN_HEIGHT / 5 * 3;
    }
    return USERVIEW_CELL_HEIGHT;
}


#pragma mark - 设置按钮的响应事件
- (void)setButtonAction
{
    SetViewController *setVC = [[SetViewController alloc]init];
    UINavigationController *setNav = [[UINavigationController alloc]initWithRootViewController:setVC];
    [self presentViewController:setNav animated:YES completion:nil];
}

#pragma mark - cell的点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (_userCollects.count != 0) {
        ProDetailViewController *proVC = [[ProDetailViewController alloc]init];
        if (_userCollects.count != 0) {
            proVC.proDetailUrl = [NSString stringWithFormat:@"%@", [_userCollects[indexPath.row] url]];
            proVC.proDetailInfo = [NSString stringWithFormat:@"%@", [_userCollects[indexPath.row] info]];
            proVC.proDetailImage = [NSString stringWithFormat:@"%@", [_userCollects[indexPath.row] image]];
        }
        [self.navigationController pushViewController:proVC animated:YES];
    }
}
//#pragma mark - 让本行row可以进行编辑
//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
//    return YES;
//}
//#pragma mark - 编辑时刻的row样式
//- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return UITableViewCellEditingStyleDelete;
//}
//#pragma mark - 编辑完成  修改UI 删除选中的row
//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    // 判断是否是删除状态
//    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        // 修改数据源
//        [_userCollects removeObjectAtIndex:indexPath.row];
//        // 修改UI 删除该row
//        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationRight];
//    }
//}


#pragma mark - 视图开始滚动时候的方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat content = scrollView.contentOffset.y;
    CGRect newFrame = _userView.userImage.frame;
    // 判断当前偏移状态
    if (content < 0) {
        [_userView.userTableView reloadData];
        
    } else if (content >= 0 && content < USER_DETAIL_HIGHT) {
        // imageView的frame改变要等于偏移量
        newFrame.origin.y = USER_IMAGEVIEW_ORIGIN_Y - content;
         _userView.userImage.frame = newFrame;
//        隐藏头像和登录名
        if (content <= 60) {
            _userView.headImageButton.alpha = 1 - (content / 60);
            _userView.headName.alpha = 1 - (content / 60);
            _userView.userTopNavTitle.alpha = (content / 60);
            
        }
        
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
